<?php

namespace Common\Bundle\LibraryBundle\Validator;

use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class MinBonneReponseValidator extends ConstraintValidator{

    private $container;

    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    public function validate($qcm, Constraint $constraint)
    {
        foreach($qcm->getQuestions() as $question)
        {
            $typeQuestion = $question->getTypeQuestion()->getId();
            $cptBonneReponse = 0;
            foreach($question->getReponses() as $reponse)
            {
                if($reponse->getEstCorrecte() == 1)
                {
                    $cptBonneReponse++;
                }
            }
            if($cptBonneReponse == 0)
            {
                $message = 'Question : '.$question->getLibelle().' | Au moins une des réponses doit être correcte';
                $this->context->addViolation($constraint->message, array('%message%' => $message));
                break;
            }
            elseif(($cptBonneReponse == 1 && $typeQuestion == $this->container->getParameter('id_choix_multiple')) or ($cptBonneReponse > 1 && $typeQuestion == $this->container->getParameter('id_choix_unique')))
            {
                $message = 'Question : '.$question->getLibelle().' | Le nombre de bonne réponse choisi ne correspond pas au type de question séléctionné';
                $this->context->addViolation($constraint->message, array('%message%' => $message));
                break;
            }
        }
    }
} 